home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP05.ZIP / CHAP05 / SCHMOO / SCHMOO.CPP < prev    next >
C/C++ Source or Header  |  1993-06-15  |  15KB  |  658 lines

  1. /*
  2.  * SCHMOO.CPP
  3.  * Chapter 5 Modifications
  4.  * WinMain and CSchmooFrame implementations.
  5.  *
  6.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  7.  *
  8.  * Kraig Brockschmidt, Software Design Engineer
  9.  * Microsoft Systems Developer Relations
  10.  *
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16. #define INITGUIDS
  17. #include "schmoo.h"
  18.  
  19.  
  20.  
  21.  
  22. /*
  23.  * WinMain
  24.  *
  25.  * Purpose:
  26.  *  Main entry point of application.   Should register the app class
  27.  *  if a previous instance has not done so and do any other one-time
  28.  *  initializations.
  29.  */
  30.  
  31. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR pszCmdLine, int nCmdShow)
  32.     {
  33.     LPCSchmooFrame  pFR;
  34.     FRAMEINIT       fi;
  35.     WPARAM          wRet;
  36.  
  37.     //CHAPTER5MOD
  38.    #ifndef WIN32
  39.     SetMessageQueue(96);
  40.    #endif
  41.     //End CHAPTER5MOD
  42.  
  43.     //Attempt to allocate and initialize the application
  44.     pFR=new CSchmooFrame(hInst, hPrev, pszCmdLine, nCmdShow);
  45.  
  46.     fi.idsMin=IDS_FRAMEMIN;
  47.     fi.idsMax=IDS_FRAMEMAX;
  48.     fi.idsStatMin=IDS_STATMESSAGEMIN;
  49.     fi.idsStatMax=IDS_STATMESSAGEMAX;
  50.     fi.idStatMenuMin=ID_MENUFILE;
  51.     fi.idStatMenuMax=ID_MENUHELP;
  52.     fi.iPosWindowMenu=WINDOW_MENU;
  53.     fi.cMenus=CMENUS;
  54.  
  55.     //If we can initialize pFR, start chugging messages
  56.     if (pFR->FInit(&fi))
  57.         wRet=pFR->MessageLoop();
  58.  
  59.     delete pFR;
  60.     return wRet;
  61.     }
  62.  
  63.  
  64.  
  65.  
  66. /*
  67.  * CSchmooFrame::CSchmooFrame
  68.  * CSchmooFrame::~CSchmooFrame
  69.  *
  70.  * Constructor Parameters:
  71.  *  hInst           HINSTANCE from WinMain
  72.  *  hInstPrev       HINSTANCE from WinMain
  73.  *  pszCmdLine      LPSTR from WinMain
  74.  *  nCmdShow        int from WInMain
  75.  */
  76.  
  77. CSchmooFrame::CSchmooFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  78.     , LPSTR pszCmdLine, int nCmdShow)
  79.     : CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
  80.     {
  81.     UINT        i;
  82.  
  83.     for (i=0; i<5; i++)
  84.         m_hBmpLines[i]=NULL;
  85.  
  86.     m_fInitialized=FALSE;
  87.     return;
  88.     }
  89.  
  90.  
  91. CSchmooFrame::~CSchmooFrame(void)
  92.     {
  93.     UINT        i;
  94.  
  95.     for (i=0; i<5; i++)
  96.         {
  97.         if (NULL!=m_hBmpLines[i])
  98.             DeleteObject(m_hBmpLines[i]);
  99.         }
  100.  
  101.     if (m_fInitialized)
  102.         CoUninitialize();
  103.  
  104.     return;
  105.     }
  106.  
  107.  
  108.  
  109.  
  110. /*
  111.  * CSchmooFrame::FInit
  112.  *
  113.  * Purpose:
  114.  *  Call CoInitialize then calling down into the base class
  115.  *  initialization.
  116.  *
  117.  * Parameters:
  118.  *  pFI             LPFRAMEINIT containing initialization parameters.
  119.  *
  120.  * Return Value:
  121.  *  BOOL            TRUE if initialization succeeded, FALSE otherwise.
  122.  */
  123.  
  124. BOOL CSchmooFrame::FInit(LPFRAMEINIT pFI)
  125.     {
  126.     DWORD       dwVer;
  127.  
  128.     dwVer=CoBuildVersion();
  129.  
  130.     if (rmm!=HIWORD(dwVer))
  131.         return FALSE;
  132.  
  133.     if (FAILED(CoInitialize(NULL)))
  134.         return FALSE;
  135.  
  136.     m_fInitialized=TRUE;
  137.  
  138.     return CFrame::FInit(pFI);
  139.     }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. /*
  147.  * CSchmooFrame::CreateCClient
  148.  *
  149.  * Purpose:
  150.  *  Constructs a new client specific to the application.
  151.  *
  152.  * Parameters:
  153.  *  None
  154.  *
  155.  * Return Value:
  156.  *  LPCClient       Pointer to the new client object.
  157.  */
  158.  
  159. LPCClient CSchmooFrame::CreateCClient(void)
  160.     {
  161.     return (LPCClient)(new CSchmooClient(m_hInst));
  162.     }
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. /*
  172.  * CSchmooFrame::FRegisterAllClasses
  173.  *
  174.  * Purpose:
  175.  *  Registers all classes used in this application.
  176.  *
  177.  * Parameters:
  178.  *  None
  179.  *
  180.  * Return Value:
  181.  *  BOOL            TRUE if registration succeeded, FALSE otherwise.
  182.  */
  183.  
  184. BOOL CSchmooFrame::FRegisterAllClasses(void)
  185.     {
  186.     WNDCLASS        wc;
  187.  
  188.     //First let the standard frame do its thing
  189.     if (!CFrame::FRegisterAllClasses())
  190.         return FALSE;
  191.  
  192.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  193.     wc.hInstance     = m_hInst;
  194.     wc.cbClsExtra    = 0;
  195.     wc.lpfnWndProc   = PolylineWndProc;
  196.     wc.cbWndExtra    = CBPOLYLINEWNDEXTRA;
  197.     wc.hIcon         = NULL;
  198.     wc.hCursor       = LoadCursor(NULL, IDC_CROSS);
  199.     wc.hbrBackground = NULL;
  200.     wc.lpszMenuName  = NULL;
  201.     wc.lpszClassName = SZCLASSPOLYLINE;
  202.  
  203.     if (!RegisterClass(&wc))
  204.         return FALSE;
  205.  
  206.     return TRUE;
  207.     }
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214. /*
  215.  * CSchmooFrame::FPreShowInit
  216.  *
  217.  * Purpose:
  218.  *  Called from FInit before intially showing the window.  We do whatever
  219.  *  else we want here, modifying nCmdShow as necessary which affects
  220.  *  ShowWindow in FInit.
  221.  *
  222.  * Parameters:
  223.  *  None
  224.  *
  225.  * Return Value:
  226.  *  BOOL            TRUE if this initialization succeeded, FALSE otherwise.
  227.  */
  228.  
  229. BOOL CSchmooFrame::FPreShowInit(void)
  230.     {
  231.     CreateLineMenu();
  232.     CheckLineSelection(IDM_LINESOLID);
  233.     m_pGB->Check(IDM_LINESOLID, TRUE);
  234.     return TRUE;
  235.     }
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242. /*
  243.  * CSchmooFrame::CreateLineMenu
  244.  *
  245.  * Purpose:
  246.  *  Initializes the bitmaps used to create the Line menu and replaces
  247.  *  the text items defined in the application resources with these
  248.  *  bitmaps.  Note that the contents of m_hBmpLines must be cleaned
  249.  *  up when the application terminates.
  250.  *
  251.  * Parameters:
  252.  *  None
  253.  *
  254.  * Return Value:
  255.  *  None
  256.  */
  257.  
  258. void CSchmooFrame::CreateLineMenu(void)
  259.     {
  260.     HMENU       hMenu;
  261.     HDC         hDC, hMemDC;
  262.     HPEN        hPen;
  263.     HGDIOBJ     hObj;
  264.     TEXTMETRIC  tm;
  265.     UINT        i, cx, cy;
  266.  
  267.  
  268.     hMenu=GetSubMenu(GetMenu(m_hWnd), 3);   //Line menu.
  269.     hDC=GetDC(m_hWnd);
  270.  
  271.     //Create each line in a menu item 8 chars wide, one char high.
  272.     GetTextMetrics(hDC, &tm);
  273.     cx=tm.tmAveCharWidth*8;
  274.     cy=tm.tmHeight;
  275.  
  276.     //Create a memory DC in which to draw lines, and bitmaps for each line.
  277.     hMemDC=CreateCompatibleDC(hDC);
  278.     ReleaseDC(m_hWnd, hDC);
  279.  
  280.     for (i=0; i<5; i++)
  281.         {
  282.         m_hBmpLines[i]=CreateCompatibleBitmap(hMemDC, cx, cy);
  283.         SelectObject(hMemDC, m_hBmpLines[i]);
  284.  
  285.         PatBlt(hMemDC, 0, 0, cx, cy, WHITENESS);
  286.  
  287.         hPen=CreatePen(i, 1, 0L);           //i==line style like PS_SOLID
  288.         hObj=SelectObject(hMemDC, hPen);
  289.  
  290.         MoveTo(hMemDC, 0,  cy/2);
  291.         LineTo(hMemDC, cx, cy/2);
  292.  
  293.         ModifyMenu(hMenu, IDM_LINEMIN+i, MF_BYCOMMAND | MF_BITMAP
  294.             , IDM_LINEMIN+i, (LPCSTR)MAKELONG(m_hBmpLines[i], 0));
  295.  
  296.         SelectObject(hMemDC, hObj);
  297.         DeleteObject(hPen);
  298.         }
  299.  
  300.     CheckMenuItem(hMenu, IDM_LINESOLID, MF_CHECKED);
  301.     DeleteDC(hMemDC);
  302.  
  303.     return;
  304.     }
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314. /*
  315.  * CSchmooFrame::CreateGizmos
  316.  *
  317.  * Purpose:
  318.  *  Procedure to create all the necessary gizmobar buttons.
  319.  *
  320.  * Parameters:
  321.  *  None
  322.  *
  323.  * Return Value:
  324.  *  UINT            Number of gizmos added to the bar.
  325.  */
  326.  
  327. UINT CSchmooFrame::CreateGizmos(void)
  328.     {
  329.     UINT            iLast;
  330.     UINT            uState=GIZMO_NORMAL;
  331.     UINT            utCmd =GIZMOTYPE_BUTTONCOMMAND;
  332.     UINT            utEx  =GIZMOTYPE_BUTTONATTRIBUTEEX;
  333.  
  334.     //Insert the standard ones.
  335.     iLast=CFrame::CreateGizmos();
  336.  
  337.     //Insert File Import in the 5th position and account for it in iLast.
  338.     m_pGB->Add(utCmd, 4, IDM_FILEIMPORT, m_dxB, m_dyB, NULL, m_hBmp, 2, uState);
  339.     iLast++;
  340.  
  341.     //Separator
  342.     m_pGB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB, NULL, NULL, 0, uState);
  343.  
  344.     //For the Background bitmap, preserve our use of black (part of the image)
  345.     m_pGB->Add(utCmd, iLast++, IDM_COLORBACKGROUND, m_dxB, m_dyB, NULL, m_hBmp, 3
  346.                , GIZMO_NORMAL | PRESERVE_BLACK);
  347.  
  348.     m_pGB->Add(utCmd, iLast++, IDM_COLORLINE, m_dxB, m_dyB, NULL, m_hBmp, 4, uState);
  349.  
  350.     //Separator
  351.     m_pGB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB, NULL, NULL, 0, uState);
  352.  
  353.     //Line styles.
  354.     m_pGB->Add(utEx, iLast++, IDM_LINESOLID,      m_dxB, m_dyB, NULL, m_hBmp, 5, uState);
  355.     m_pGB->Add(utEx, iLast++, IDM_LINEDASH,       m_dxB, m_dyB, NULL, m_hBmp, 6, uState);
  356.     m_pGB->Add(utEx, iLast++, IDM_LINEDOT,        m_dxB, m_dyB, NULL, m_hBmp, 7, uState);
  357.     m_pGB->Add(utEx, iLast++, IDM_LINEDASHDOT,    m_dxB, m_dyB, NULL, m_hBmp, 8, uState);
  358.     m_pGB->Add(utEx, iLast++, IDM_LINEDASHDOTDOT, m_dxB, m_dyB, NULL, m_hBmp, 9, uState);
  359.  
  360.     return iLast;
  361.     }
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370. /*
  371.  * CSchmooFrame::OnCommand
  372.  *
  373.  * Purpose:
  374.  *  WM_COMMAND handler for the Sch